package com.zhentao.controller;

import com.zhentao.pojo.Content;
import com.zhentao.pojo.ContentChapter;
import com.zhentao.pojo.Lecturer;
import com.zhentao.pojo.Organization;
import com.zhentao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 内容章节表 前端控制器
 * </p>
 *
 * @author gyz
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/contentChapter")
public class ContentChapterController {
    @Autowired
    ContentService contentService;
    @Autowired
    ContentChapterService contentChapterService;
    @Autowired
    ReflexZoneService reflexZoneService;
    @Autowired
    LecturerService lecturerService;
    @Autowired
    OrganizationService organizationService;


    //获取课程信息以及内容章节
    @RequestMapping("getCont")
    public Object getCont() {
        List<Content> list = contentService.list();
        List<ContentChapter> list1 = contentChapterService.list();
        for (Content content : list) {
            Lecturer lecturer = lecturerService.getById(content.getLecturerId());
            Organization organization = organizationService.getById(content.getOrgId());
            List<ContentChapter> list2 = new ArrayList<>();
            for (ContentChapter contentChapter : list1) {
                if(contentChapter.getContentId()==content.getId()){
                    list2.add(contentChapter);
                }
            }
            content.setChapterList(list2);
            content.setLecturer(lecturer);
            content.setOrganization(organization);
        }
        return list;
    }
    //推荐课程信息以及内容章节
    @RequestMapping("getCont1")
    public Object getCont1() {
        List<Content> list = contentService.list();
        List<ContentChapter> list1 = contentChapterService.list();
        for (Content content : list) {
            Lecturer lecturer = lecturerService.getById(content.getLecturerId());
            Organization organization = organizationService.getById(content.getOrgId());
            List<ContentChapter> list2 = new ArrayList<>();
            for (ContentChapter contentChapter : list1) {
                if(contentChapter.getContentId()==content.getId()){
                    list2.add(contentChapter);
                }
            }
            content.setChapterList(list2);
            content.setLecturer(lecturer);
            content.setOrganization(organization);
        }
        int size = Math.min(list.size(), 3); // 防止列表长度不足3时越界
        return list.subList(0, size);
    }
    
    //根据分类获取课程信息以及内容章节
    @RequestMapping("getContByCategory")
    public Object getContByCategory(@RequestParam("categoryId") Long categoryId) {
        List<Content> allList = contentService.list();
        List<ContentChapter> allChapters = contentChapterService.list();
        
        // 筛选指定分类的课程
        List<Content> filteredList = new ArrayList<>();
        for (Content content : allList) {
            if (content.getCategoryId().equals(categoryId)) {
                filteredList.add(content);
            }
        }
        
        // 为每个课程添加章节、讲师和机构信息
        for (Content content : filteredList) {
            Lecturer lecturer = lecturerService.getById(content.getLecturerId());
            Organization organization = organizationService.getById(content.getOrgId());
            List<ContentChapter> contentChapters = new ArrayList<>();
            
            for (ContentChapter contentChapter : allChapters) {
                if(contentChapter.getContentId().equals(content.getId())){
                    contentChapters.add(contentChapter);
                }
            }
            
            content.setChapterList(contentChapters);
            content.setLecturer(lecturer);
            content.setOrganization(organization);
        }
        
        return filteredList;
    }
}
